package com.lida.zwservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lida.zwservice.entity.AreaRegionVo;
import com.lida.zwservice.entity.CityRegionVo;
import com.lida.zwservice.entity.ProvinceRegionVo;
import com.lida.zwservice.entity.SysRegion;
import com.lida.zwservice.mapper.SysRegionMapper;
import com.lida.zwservice.service.SysRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiDa
 * @since 2022-01-27
 */
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements SysRegionService {

    @Override
    public List<ProvinceRegionVo> getregion(String parentId) {
        //查询省级目录
        QueryWrapper<SysRegion> provincewrapper=new QueryWrapper<>();
        provincewrapper.eq("ParentId",parentId);
        List<SysRegion> provinceList=baseMapper.selectList(provincewrapper);

        List<ProvinceRegionVo> provinceListvo=new ArrayList<>();
        //封装省级目录
        for(int i=0; i<provinceList.size(); i++){
            SysRegion provinceRegion =provinceList.get(i);
            ProvinceRegionVo province=new ProvinceRegionVo();
            BeanUtils.copyProperties(provinceRegion,province);
            provinceListvo.add(province);

            //查询市级目录
            QueryWrapper<SysRegion> citywrapper=new QueryWrapper<>();
            citywrapper.eq("ParentId",provinceRegion.getId());
            List<SysRegion> cityList=baseMapper.selectList(citywrapper);

            List<CityRegionVo> CityRegionVo=new ArrayList<>();
            //封装市级目录
            for(int j=0;j<cityList.size();j++){
                SysRegion cityRegion =cityList.get(j);
                CityRegionVo city=new CityRegionVo();
                BeanUtils.copyProperties(cityRegion,city);
                CityRegionVo.add(city);

                //查询区级目录
                QueryWrapper<SysRegion> areawrapper =new QueryWrapper<>();
                areawrapper.eq("ParentId",cityRegion.getId());
                List<SysRegion> areaList=baseMapper.selectList(areawrapper);

                List<AreaRegionVo> AreaRegionVo=new ArrayList<>();
                //封装区级目录
                for (int k=0;k<areaList.size();k++){
                    SysRegion areaRegion =areaList.get(k);
                    AreaRegionVo area=new AreaRegionVo();
                    BeanUtils.copyProperties(areaRegion,area);
                    AreaRegionVo.add(area);
                }
                city.setAreaRegions(AreaRegionVo);
            }
            province.setCityList(CityRegionVo);
        }

        return provinceListvo;
    }
}
